package com.monitor.ui;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 资源中心面板，用于管理资源库、白名单和黑名单
 */
public class ResourcePanel extends JPanel {
    private static final Color MAIN_COLOR = new Color(0, 120, 215);
    
    private JTextArea contentArea;
    private JButton resourceBtn;
    private JButton whiteListBtn;
    private JButton blackListBtn;
    
    private List<String> resources;
    private List<String> whitelist;
    private List<String> blacklist;
    
    private String currentTab = "resource";
    
    public ResourcePanel() {
        // 初始化数据
        resources = new ArrayList<>();
        whitelist = new ArrayList<>();
        blacklist = new ArrayList<>();
        
        // 加载默认数据
        loadDefaultData();
        
        // 设置面板
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);
        
        // 创建标题面板
        JPanel titlePanel = createTitlePanel("资源中心");
        add(titlePanel, BorderLayout.NORTH);
        
        // 创建内容面板
        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.setBackground(Color.WHITE);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 创建选项卡面板
        JPanel tabPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        tabPanel.setBackground(Color.WHITE);
        
        resourceBtn = createTabButton("资源库");
        whiteListBtn = createTabButton("白名单");
        blackListBtn = createTabButton("黑名单");
        
        resourceBtn.setBackground(MAIN_COLOR);
        resourceBtn.setForeground(Color.WHITE);
        
        tabPanel.add(resourceBtn);
        tabPanel.add(whiteListBtn);
        tabPanel.add(blackListBtn);
        
        // 创建内容区域
        contentArea = new JTextArea();
        contentArea.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        contentArea.setLineWrap(true);
        contentArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(contentArea);
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        
        // 显示默认内容
        showResources();
        
        // 创建按钮区域
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setBackground(Color.WHITE);
        
        JButton addBtn = new JButton("添加");
        JButton editBtn = new JButton("编辑");
        JButton deleteBtn = new JButton("删除");
        JButton applyBtn = new JButton("应用");
        
        addBtn.addActionListener(e -> addItem());
        editBtn.addActionListener(e -> editItem());
        deleteBtn.addActionListener(e -> deleteItem());
        applyBtn.addActionListener(e -> applyChanges());
        
        buttonPanel.add(addBtn);
        buttonPanel.add(editBtn);
        buttonPanel.add(deleteBtn);
        buttonPanel.add(applyBtn);
        
        contentPanel.add(tabPanel, BorderLayout.NORTH);
        contentPanel.add(scrollPane, BorderLayout.CENTER);
        contentPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        add(contentPanel, BorderLayout.CENTER);
    }
    
    private JPanel createTitlePanel(String title) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(MAIN_COLOR);
        panel.setPreferredSize(new Dimension(800, 40));
        
        JLabel titleLabel = new JLabel(title, JLabel.CENTER);
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        
        panel.add(titleLabel, BorderLayout.CENTER);
        
        return panel;
    }
    
    private JButton createTabButton(String text) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        button.setFocusPainted(false);
        button.setBackground(Color.WHITE);
        button.setForeground(Color.BLACK);
        button.setBorderPainted(false);
        
        button.addActionListener(e -> switchTab(text));
        
        return button;
    }
    
    private void switchTab(String tabName) {
        // 重置所有按钮样式
        resourceBtn.setBackground(Color.WHITE);
        resourceBtn.setForeground(Color.BLACK);
        whiteListBtn.setBackground(Color.WHITE);
        whiteListBtn.setForeground(Color.BLACK);
        blackListBtn.setBackground(Color.WHITE);
        blackListBtn.setForeground(Color.BLACK);
        
        // 设置选中按钮样式
        switch (tabName) {
            case "资源库":
                resourceBtn.setBackground(MAIN_COLOR);
                resourceBtn.setForeground(Color.WHITE);
                currentTab = "resource";
                showResources();
                break;
            case "白名单":
                whiteListBtn.setBackground(MAIN_COLOR);
                whiteListBtn.setForeground(Color.WHITE);
                currentTab = "whitelist";
                showWhitelist();
                break;
            case "黑名单":
                blackListBtn.setBackground(MAIN_COLOR);
                blackListBtn.setForeground(Color.WHITE);
                currentTab = "blacklist";
                showBlacklist();
                break;
        }
    }
    
    private void loadDefaultData() {
        // 加载默认资源
        resources.add("教育资源1: http://www.edu.cn/resource1");
        resources.add("教育资源2: http://www.edu.cn/resource2");
        resources.add("学习资料: http://www.learning.com/materials");
        
        // 加载默认白名单
        whitelist.add("edu.cn");
        whitelist.add("gov.cn");
        whitelist.add("wikipedia.org");
        
        // 加载默认黑名单
        blacklist.add("gambling.com");
        blacklist.add("adult-content.com");
        blacklist.add("violence-games.com");
    }
    
    private void showResources() {
        StringBuilder sb = new StringBuilder();
        for (String resource : resources) {
            sb.append(resource).append("\n");
        }
        contentArea.setText(sb.toString());
    }
    
    private void showWhitelist() {
        StringBuilder sb = new StringBuilder();
        for (String site : whitelist) {
            sb.append(site).append("\n");
        }
        contentArea.setText(sb.toString());
    }
    
    private void showBlacklist() {
        StringBuilder sb = new StringBuilder();
        for (String site : blacklist) {
            sb.append(site).append("\n");
        }
        contentArea.setText(sb.toString());
    }
    
    private void addItem() {
        String item = JOptionPane.showInputDialog(this, "请输入要添加的内容：");
        if (item != null && !item.trim().isEmpty()) {
            switch (currentTab) {
                case "resource":
                    resources.add(item.trim());
                    showResources();
                    break;
                case "whitelist":
                    whitelist.add(item.trim());
                    showWhitelist();
                    break;
                case "blacklist":
                    blacklist.add(item.trim());
                    showBlacklist();
                    break;
            }
        }
    }
    
    private void editItem() {
        String content = contentArea.getSelectedText();
        if (content == null || content.trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "请先选择要编辑的内容", "提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        
        String newContent = JOptionPane.showInputDialog(this, "编辑内容：", content);
        if (newContent != null && !newContent.trim().isEmpty()) {
            String fullText = contentArea.getText();
            fullText = fullText.replace(content, newContent);
            contentArea.setText(fullText);
            
            // 更新数据列表
            updateDataFromText();
        }
    }
    
    private void deleteItem() {
        String content = contentArea.getSelectedText();
        if (content == null || content.trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "请先选择要删除的内容", "提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        
        int result = JOptionPane.showConfirmDialog(this, "确定要删除所选内容吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            String fullText = contentArea.getText();
            fullText = fullText.replace(content + "\n", "").replace(content, "");
            contentArea.setText(fullText);
            
            // 更新数据列表
            updateDataFromText();
        }
    }
    
    private void updateDataFromText() {
        String[] lines = contentArea.getText().split("\n");
        
        switch (currentTab) {
            case "resource":
                resources.clear();
                for (String line : lines) {
                    if (!line.trim().isEmpty()) {
                        resources.add(line.trim());
                    }
                }
                break;
            case "whitelist":
                whitelist.clear();
                for (String line : lines) {
                    if (!line.trim().isEmpty()) {
                        whitelist.add(line.trim());
                    }
                }
                break;
            case "blacklist":
                blacklist.clear();
                for (String line : lines) {
                    if (!line.trim().isEmpty()) {
                        blacklist.add(line.trim());
                    }
                }
                break;
        }
    }
    
    private void applyChanges() {
        // 从文本区域更新数据
        updateDataFromText();
        
        // 显示确认消息
        JOptionPane.showMessageDialog(this, "更改已应用", "成功", JOptionPane.INFORMATION_MESSAGE);
    }
    
    /**
     * 获取资源列表
     */
    public List<String> getResources() {
        return new ArrayList<>(resources);
    }
    
    /**
     * 获取白名单列表
     */
    public List<String> getWhitelist() {
        return new ArrayList<>(whitelist);
    }
    
    /**
     * 获取黑名单列表
     */
    public List<String> getBlacklist() {
        return new ArrayList<>(blacklist);
    }
}